<?php

require_once("configure.php");

$date = $_POST["date"];
$user = $_POST["user"];

$MySqlLinkId = mysql_connect(DBHost, DBUser, DBPwd);
if($MySqlLinkId == FALSE)
{
	echo("({ErrCode:1, ErrMsg:'链接数据库失败！'})");
	exit();
}

if(FALSE == mysql_select_db(DB, $MySqlLinkId))
{
	echo("({ErrCode:3, ErrMsg:".json_encode(mysql_error($MySqlLinkId))."})");
	mysql_close($MySqlLinkId);
	exit();
}

$BillData = Array();
$sql = sprintf("SELECT Bill.Id, PayAmount, Name, Remark, CreateTime FROM Bill INNER JOIN BillCategory ON Bill.CategoryId = BillCategory.Id WHERE Bill.AccountName='%s' AND CreateTime>='%s-01' AND CreateTime<='%s-31' ORDER BY CreateTime ASC", $user, $date, $date);
$result = mysql_query($sql, $MySqlLinkId);
while(($row = mysql_fetch_row($result)))
{
	$BillData[] = Array($row[0], ((float)$row[1]) / 100, $row[2], $row[3], $row[4]);
}
mysql_free_result($result);
mysql_close($MySqlLinkId);

$BillCategory = Array();
$CurrentMonthBillData = Array();
foreach($BillData as $d)
{
	$BillCategory[$d[2]] += $d[1];
	$CurrentMonthBillData[] = $d;
}

$chart2_data = Array();
foreach($BillCategory as $k => $bc)
{
	$chart2_data[] = Array($k, $bc);
}
echo("({ErrCode:0, ErrMsg:'', BillData:".json_encode($CurrentMonthBillData).", ChartXml:".json_encode($chart2_data)."})");

?>